<template>
    <!-- 日消费限额设置 -->
    <div class="dailyLimitSet" v-loading="isLoading">
        <cardComponent pageTitle="日消费限额设置">
            <template slot="cardBody">
                <el-form :model="searchFormOne" size="small" label-width="50%">
                    <el-col :span="8">
                        <el-form-item label="账户类别:" label-position="left" prop="mainCardNo">
                            <el-input disabled v-model="accountType" style="width: 200px;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="卡号：" label-position="left" prop="CARDNO">
                            <el-input v-model="searchFormOne.CARDNO" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <div class="btnSrue">
                            <el-button
                                type="primary"
                                size="small"
                                @click="hanleSure"
                                icon="el-icon-circle-check"
                                v-preventReClick
                            >确定</el-button>
                        </div>
                    </el-col>
                </el-form>
                <div class="dialogVisible" v-show="dialogVisible">
                    <el-form :model="searchForm" size="small" label-width="50%" ref="searchForm">
                        <el-form-item label="账户类别：" label-position="left">个人卡账户</el-form-item>

                        <el-form-item label="卡号：" label-position="left" prop="CARDNO">
                            <el-input v-model="searchForm.CARDNO" disabled></el-input>
                        </el-form-item>

                        <el-form-item label="日消费限额：" label-position="left" prop="MAXPAMT">
                            <el-input v-model="searchForm.MAXPAMT" disabled></el-input>
                        </el-form-item>
                    </el-form>
                    <el-form
                        :model="searchFormOne"
                        size="small"
                        label-width="50%"
                        :rules="rules"
                        ref="searchForm"
                    >
                        <el-form-item label="请输入新的值：" label-position="left" prop="newMaxPAMT">
                            <el-input v-model="searchFormOne.newMaxPAMT"></el-input>
                        </el-form-item>
                        <div class="btnBox">
                            <el-button type="primary" size="small" @click="hanleUpdate">修改</el-button>
                            <!-- <el-button type="primary" size="small" @click="handleClose">关闭</el-button> -->
                        </div>
                    </el-form>
                </div>
            </template>
        </cardComponent>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import { getAccountTypeName } from '@/utils/Common';
import { RegConfig } from '@/utils/RegConfig';
import cardComponent from '@/components/cardComponent/cardComponent.vue';

export default {
    name: 'dailyLimitSet',
    components: {
        cardComponent,
    },
    data() {
        const validatorPass = (rule, value, callback) => {
            if (value > this.amount) {
                callback(new Error('高于账户额度，无法调整'));
            } else {
                callback();
            }
        };
        return {
            searchFormOne: {
                CARDNO: '6250520500003711',
                newMaxPAMT: '', // 新的日消费限额
            },
            searchForm: { // 搜索表单字段
                CARDNO: '', // 卡号
                MAXPAMT: '', // 日消费限额
            },
            dialogVisible: false,
            // 判断是查询还是修改
            searchOrUpdate: '0',
            // 每日消费透支，修改时请求，识别交易类型
            dailyMitLable: '',
            accountType: '',
            isLoading: false,
            // 表单验证
            rules: {
                newMaxPAMT: [
                    { required: true, message: '请输入新内容', trigger: 'blur' },
                    { pattern: RegConfig.integerAmount, message: '请输入整数，首位不可为0', trigger: 'blur' },
                ],
            },
        };
    },
    filters: {

    },
    async mounted() {
        this.searchFormOne.CARDNO = this.cardInfo_3004.CARDNO;
        this.amount = this.cardInfo_3004.AVLIMT;
        this.accountType = await getAccountTypeName(this.cardList_3601, this.cardInfo_3004, this);
    },
    computed: mapState({
        cardInfo_3004: state => state.common.cardInfo_3004,
        cardList_3601: state => state.common.cardList_3601,
        defaultParams: state => state.common.defaultParams,
        defaultpinFlag: state => state.common.pinFlag,
    }),
    methods: {
        // 点击确定按钮进行查询
        hanleSure() {
            this.searchOrUpdate = '0';
            this.dialogVisible = true;
            this.hanleTableData();
            this.dailyMitLable = '';
        },
        // 点击修改按钮
        hanleUpdate() {
            this.searchOrUpdate = '1';
            this.dailyMitLable = 6;
            this.$refs.searchForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认修改？', '提示', { customClass: 'warning', type: 'warning' }).then(() => {
                        this.hanleTableData();
                        this.searchFormOne.newMaxPAMT = '';
                    }).catch(() => {

                    });
                } else {

                }
            });
        },
        hanleTableData() {
            console.log(this.searchOrUpdate);
            this.isLoading = true;
            const appBody = {
                CARDNO: this.cardInfo_3004.CARDNO || '', // 卡号
                PINFLAG: this.defaultpinFlag, // 是否检查密码标志
                OPTION: this.searchOrUpdate, // 修改选项   0为查询  1为修改
                SERTYPE: this.dailyMitLable, // 服务类型  查询时无作用
                CHGPARAM: this.searchFormOne.newMaxPAMT, // 修改内容，查询时无作用
            };
            const defaultParams = JSON.parse(JSON.stringify(this.defaultParams));
            defaultParams.body.appHead.tranCode = '4058';
            defaultParams.body.appBody = appBody;
            // 4058交易
            this.$post.common.transPort(defaultParams).then((res) => {
                this.isLoading = false;
                if (res.status === 200) {
                    const result = JSON.parse(res.data.jsonRes);
                    console.log(result, 111111);
                    if (result.sysHead.rspCd === '000000') {
                        this.searchForm = result.body.appBody || [];
                        if (appBody.OPTION === '1') {
                            this.$alert('修改成功!', '提示', {
                                customClass: 'success', type: 'success',
                            });
                        }
                    } else {
                        const errorMsg = `错误代码：${result.sysHead.rspCd}, 错误信息：${result.sysHead.rspMsg}`;
                        this.$alert(errorMsg, '提示', {
                            customClass: 'error', type: 'error',
                        });
                    }
                } else {
                    this.$alert('请求失败！', '提示', {
                        customClass: 'error', type: 'error',
                    });
                }
            });
        },

    },
};
</script>

<style lang='scss'>
.dailyLimitSet {
    width: 98%;
    min-height: 100%;
    background: #fff;
    // padding:10px;
    box-sizing: border-box;
    border-radius: 5px;

    /* //头部内容 */
    .tab-header {
        width: 100%;
        text-align: left;
        span {
            font-size: 14px;
        }
        .headerTitle {
            font-size: 16px;
            margin: 10px 0px;
            color: skyblue;
        }
    }
    .btnBox {
        text-align: center;
    }
    .btnSrue {
        margin-left: 25px;
    }
    .dialogVisible {
        margin: 108px auto 0;
        .el-form-item__content {
            text-align: left;
        }
    }
}
</style>
